package com.bracket

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.share_list_item.view.*

class ShareAdapter : RecyclerView.Adapter<ShareAdapter.ViewHolder> {

    private lateinit var apps: List<AppInfo>

    private lateinit var onItemClickListener: OnItemClickListener

    constructor(apps: List<AppInfo>, onItemClickListener: OnItemClickListener) {
        this.apps = apps
        this.onItemClickListener = onItemClickListener
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.share_list_item, parent, false))
    }

    override fun getItemCount() = apps.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var appInfo = apps[holder.adapterPosition]
        holder.itemView.app_name.text = appInfo.name
        holder.itemView.app_icon.setImageDrawable(appInfo.icon)
        holder.itemView.setOnClickListener {
            onItemClickListener.onItemClick(holder.adapterPosition)
        }
    }


    class ViewHolder : RecyclerView.ViewHolder {
        constructor(itemView: View) : super(itemView)
    }

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }
}